package nl.rainbowsheep.reisorg.cust.site.service;

import java.util.ArrayList;
import java.util.List;
import nl.rainbowsheep.reisorg.cust.site.authentication.BasicAuthInterceptor;
import nl.rainbowsheep.reisorg.cust.site.model.BurritoUser;
import nl.rainbowsheep.reisorg.cust.site.model.Customer;
import nl.rainbowsheep.reisorg.cust.site.viewmodel.CustomerViewmodel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * Service class for customer. Sends requests to Restful webservice and receives
 * requests from that webservice.
 */
@Service
public class CustomerService {

    @Autowired
    UserService userService;

    @Value("${api.url}")
    String url;

    /**
     * Send request to restful webservice to create a customer
     *
     * @param customerViewmodel
     * @return saved customer
     */
    public CustomerViewmodel insertCustomerViewmodel(CustomerViewmodel customerViewmodel) throws RuntimeException{
        BurritoUser g = customerViewmodel.getBurritoUser();
        BurritoUser gebruikerSaved = userService.saveUser(g);
        if (gebruikerSaved == null) {
            return null;
        }
        customerViewmodel.setBurritoUser(gebruikerSaved);
        customerViewmodel.getCustomer().setBurritoUserId(gebruikerSaved.getId());
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);
        return template.postForObject(url + "customer/save", customerViewmodel, CustomerViewmodel.class);
    }


    
        public CustomerViewmodel updateCustomerViewmodel(CustomerViewmodel customer) {
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);
        return template.postForObject(url + "customer/save", customer, CustomerViewmodel.class);
    }

    public Customer getCustomer(int id) {
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);

        Customer customer = template.getForObject(url + "customer/details/id/" + id, Customer.class, id);
        return customer;
    }

    public CustomerViewmodel getCustomerViewmodelByUsername(String currentUserName) {
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);

        CustomerViewmodel customerviewmodel = template.getForObject(url + "customer/details/username?username=" + currentUserName, CustomerViewmodel.class);
        return customerviewmodel;
    }

    public boolean requestResetPassword(CustomerViewmodel customerViewmodel) {
//        LOGGER.debug("SERVICE is bereikt!!");
//        LOGGER.debug(burritouser.getUsername()+ " " + customer.getEmail()); 
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);
        return template.postForObject(url + "customer/resetpassword/" + customerViewmodel.getBurritoUser().getUsername(), customerViewmodel, boolean.class);
    }

}
